import { Component, OnInit, Input, Injector } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { BehaviorSubject ,  Observable } from 'rxjs';
import { debounceTime ,  map ,  switchMap } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { importExpr } from '@angular/compiler/src/output/output_ast';
import { environment } from 'environments/environment';
import { CityListComponent } from './cityList';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../..';
import { NgZorroPageBaseComponent } from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import { StudentJoinCourseOutput } from '../../core/model/StudentJoinCourseDto';
import { isNumber } from 'util';
import { ExpressionPair, Compare, AndOr } from '../../core/model/CommonDto';
@Component({
  selector: 'app-schoolcomponent',
  templateUrl: './school.component.html',
  styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class SchoolComponent extends NgZorroPageBaseComponent implements OnInit {
  isVisible = false;
  isVisiblemm = false;
  smisVisible = false;
  allChecked = false;
  disabledButton = true;
  checkedNumber = 0;
  displayData: Array<{ name: string; age: number; address: string; checked: boolean }> = [];
  operating = false;
  dataSet = [];
  indeterminate = false;
  schoolName;
  personName;
  personPhone;
  personEmail;
  radioValueflex = 'true';
  time;
  personUserName;
  personPassword;
  EditorID;
  delesh = [];
  cityData: any;
  listUrl = 'ClassInfo/GetClassByName';
  searchChange$ = new BehaviorSubject(new SearchValue('', ''));
  optionList = [];
  pop;
  selectedUser;
  provinceData: any;
  repassword;
  reid;
  true1 = true;
  isLoading = false;
  collegelName = "";
  academeId;
  ngOnInit() {
    this.IniObserver.subscribe(sb => {
      this.getEntityList();
    });

    this.listSearchValue = [
      new ExpressionPair('enableOrDiscontinuation', "", Compare.Equal, AndOr.And),
      new ExpressionPair('isActive', "", Compare.Equal, AndOr.And),
      new ExpressionPair('schoolName', '', Compare.Contains, AndOr.And),
    ];
  }

  constructor(
    injecter: Injector,
    public http1: HttpService,
    private modalService: NzModalService,
    public http: HttpClient) {
    super(injecter,
      new BaseComponentConfig(
        "SchoolAcademeManager/GetAllSchoolManagerOutputDto",
        "",
        "",
        "",
        "",
        "id",
        "",
        "",
        [],
        "",
        { Edit: false, Dell: false },
      ));
    this.listEntities.pageSize = 10;
  }



  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean }>): void {
    this.displayData = $event;
  }

  refreshStatus1(id, e): void {
    if (e === true) {
      this.delesh.push(id);
    } else {
      for (let i = 0; i < this.delesh.length; i++) {
        if (id === this.delesh[i]) {
          this.delesh.splice(i, 1);
        }
      }
    }
    const allChecked = this.displayData.every(value => value.checked === true);
    const allUnChecked = this.displayData.every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
    this.disabledButton = !this.dataSet.some(value => value.checked);
    this.checkedNumber = this.dataSet.filter(value => value.checked).length;
  }

  checkAll(value: boolean): void {
    this.delesh = [];
    this.displayData.forEach(data => data.checked = value);
    // tslint:disable-next-line:no-shadowed-variable
    const allChecked = this.displayData.every( value => value.checked === true);
    // tslint:disable-next-line:no-shadowed-variable
    const allUnChecked = this.displayData.every(value => !value.checked);
    this.indeterminate = (!allChecked) && (!allUnChecked);
    // tslint:disable-next-line:no-shadowed-variable
    this.disabledButton = !this.dataSet.some(value => value.checked);
    // tslint:disable-next-line:no-shadowed-variable
    this.checkedNumber = this.dataSet.filter(value => value.checked).length;
    if (value === true) {
      for (let i = 0; i < this.listEntities.entities.length; i++) {
        this.delesh.push(this.listEntities.entities[i].id);
      }
    } else {
      this.delesh = [];
    }
  }

  operateData(): void {
    this.operating = true;
    setTimeout(_ => {
      this.dataSet.forEach(value => value.checked = false);
      this.refreshStatus1("2", "1");
      this.operating = false;
    }, 1000);
  }

  /**
   * 是否从远程获取数据
   */
  onSearch(value: string, url: string): void {

    this.isLoading = true;
    this.searchChange$.next(new SearchValue(value, this.listUrl));
  }
  schoolAdd() { }
  // provinceChange(value: string): void {
  //   this.selectedCity = this.cityData[value][0];
  // }

  getNewEntity(): StudentJoinCourseOutput {
    return new StudentJoinCourseOutput();
  }
  async editor(id: any) {
    const resulthx = await this.http1.get<any>('SchoolAcademeManager/GetSchoolManagerOutputDto?input=' + id);
    this.isVisible = true;
    this.academeId = resulthx.academeId;
    this.schoolName = resulthx.schoolName;
    this.personName = resulthx.personName;
    this.personPhone = resulthx.phoneNumber;
    this.personEmail = resulthx.email;
    // this.selectedProvince = resulthx.address[0];
    // this.selectedCity = resulthx.address[1];
    this.radioValueflex = resulthx.enableOrDiscontinuation.toString();
    this.time = resulthx.enableTime;
    this.personUserName = resulthx.userName;
    this.personPassword = resulthx.userPwd;
    this.EditorID = id;
    this.collegelName = resulthx.academeName;



  }
  async handleOk(type: string) {
    const data = {
      // tslint:disable-next-line:no-eval
      "academeId": this.academeId, "schoolName": this.schoolName, "enableTime": this.time, "enableOrDiscontinuation": eval(this.radioValueflex.toLowerCase()), "academeName": this.collegelName,
      "userName": this.personUserName, "userPwd": this.personPassword, "personId": 0, "isActive": true, "photoUrl": "1", "userRoleType": 1, "personName": this.personName, "id": this.EditorID, "phoneNumber": this.personPhone, "personNo": "1", "email": this.personEmail
    };


    await this.http1.post<any>('SchoolAcademeManager/UpdateSchoolManager', data);
    this.getEntityList();
    this.isVisible = false;
    this.message.create(type, `编辑成功`);
  }

  handleCancel(): void {
    this.isVisible = false;
    this.isVisiblemm = false;
    this.smisVisible = false;
  }
  async ResetPasswords(id) {
    this.reid = id;
    const repassword1 = await this.http1.get<any>('SchoolAcademeManager/GetPwd?input=' + id);
    this.repassword = repassword1;
    this.isVisiblemm = true;
  }
  handleOkmm(type: string) {
    if (this.repassword === "") {
      this.message.info('密码不能为空');
    } else {
      const data = { "id": this.reid, "userPwd": this.repassword };
      try {
        this.http1.post<any>('SchoolAcademeManager/UpdatePassword', data);
      } finally {

      }

      this.isVisiblemm = false;
      this.message.create(type, `密码修改成功`);
    }
  }
  qy(id, t) {

    const data = { "pid": id, "IsActive": t };
    this.http1.post<any>('SchoolAcademeManager/OpenOrty', data);
  }

  async deleschool() {
    this.checkedNumber = this.listEntities.entities.filter(value => value.checked).length;
    this.smisVisible = true;

  }
  async handleOksm(type: string) {
    this.smisVisible = false;
    await this.http1.post<any>('SchoolAcademeManager/DeleteSchoolManager', this.delesh);
    this.delesh = [];
    this.getEntityList();
    this.message.create(type, `删除成功`);

  }
  // 搜索
  shoolsearch() {
    // if(this.listSearchValue[1].propertyValue == '市'){
    //     this.listSearchValue[0].propertyValue = '';
    //     this.listSearchValue[1].propertyValue = '';
    // }
    this.getEntityList();
  }
  // 搜索取消
  shoolcancle() {
    this.listSearchValue = [
      new ExpressionPair('enableOrDiscontinuation', "", Compare.Equal, AndOr.And),
      new ExpressionPair('isActive', "", Compare.Equal, AndOr.And),
      new ExpressionPair('schoolName', '', Compare.Contains, AndOr.And),
    ];
  }
  // 学院管理
  schoolManagementy(id) {
    this.router.navigate(['app/schoolindex/schoolmanagement'], {
      queryParams: {
        smId: id
      }
    });
  }
  shoolsearchinput(e) {
    if (e.keyCode === 13) {
      this.shoolsearch();
    }
  }

}
